package com.nstechs.commerce.common.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    String url = null;
    String userName = null;
    String password = null;
    
    private Connection con = null;
    
    static{
    	try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    }
    public DBUtil() {
    	
    }
    
    public  DBUtil(String url, String userName, String password) {
    	this.url = url;
    	this.userName = userName;
    	this.password = password;
    }

    public Connection getConnection() {
        try {
            if(con == null || con.isClosed()){
            	con = DriverManager.getConnection(url, userName, password);
            }
        } catch (SQLException e) {
            throw new RuntimeException("get connection failed");
        }
    	return con;
    }

    public void closeConnection(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void closeStatement(Statement stmt) {
        if (null != stmt) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void closeResultSet(ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
}